#include <QDebug>
#include <QDir>

#include "operatedb.h"
#include "reqhandler.h"
#include "mytcpserver.h"

ReqHandler::ReqHandler(QObject *parent) : QObject(parent)
{

}

PDU *ReqHandler::regist()
{
    char caName[32] = { '\0' };
    char caPwd [32] = { '\0' };
    memcpy(caName, pdu->caData     , 32);
    memcpy(caPwd , pdu->caData + 32, 32);
    qDebug() << "注册请求->" << caName
             << "注册用户名:" << caName
             << "注册密码:" << caPwd;
    bool ret = OperateDB::getInstance().handleResgist(caName, caPwd);
    if(ret){
        QDir dir;
        dir.mkdir(QString("%1/%2").arg(Server::getInstance().getRootRath()).arg(caName));
    }
    qDebug() << "handleResgist ret:" << ret;
    PDU* resPdu = mkPDU(ENUM_MSG_TYPE_REGIST_RESPOND, 0);
    memcpy(resPdu->caData, &ret, sizeof(ret));

    return resPdu;
}

PDU *ReqHandler::login(QString &m_strLoginName)
{
    char caName[32] = { '\0' };
    char caPwd [32] = { '\0' };
    memcpy(caName, pdu->caData     , 32);
    memcpy(caPwd , pdu->caData + 32, 32);
    qDebug() << "登录请求->"
             << "登录用户名:" << caName
             << "登录密码:" << caPwd;
    bool ret = OperateDB::getInstance().handleLogin(caName, caPwd);
    if(ret)
    {
        m_strLoginName = caName;
    }
    qDebug() << "登录处理结果:" << ret;
    PDU* resPdu = mkPDU(ENUM_MSG_TYPE_LOGIN_RESPOND, 0);
    memcpy(resPdu->caData, &ret, sizeof(ret));
    return resPdu;
}

PDU *ReqHandler::findUser()
{
    char caName[32] = { '\0' };
    memcpy(caName, pdu->caData     , 32);
    qDebug() << "寻找的用户名:" << caName;
    int ret = OperateDB::getInstance().handleFindUser(caName);
    qDebug() << "寻找用户处理结果->" << ret;
    PDU* resPdu = mkPDU(ENUM_MSG_TYPE_FIND_USER_RESPOND, 0);
    memcpy(resPdu->caData     , &ret  , 32);
    memcpy(resPdu->caData + 32, caName, 32);
    return resPdu;
}

PDU *ReqHandler::onlineUser()
{
    QStringList res = OperateDB::getInstance().handleOnlineUser();
    uint uiMsgLen = res.size() * 32;
    PDU* resPdu = mkPDU(ENUM_MSG_TYPE_ONLINE_USER_RESPOND, uiMsgLen);
    for(int i = 0; i < res.size(); i++)
    {
        memcpy(resPdu->caMsg + i * 32, res.at(i).toStdString().c_str(), 32);
    }
    return resPdu;
}

PDU *ReqHandler::addFriend()
{
    char caCurName[32] = { '\0' };
    char caTarName[32] = { '\0' };
    memcpy(caCurName, pdu->caData     , 32);
    memcpy(caTarName, pdu->caData + 32, 32);
    qDebug() << "添加好友->"
             << "发起请求方:" << caCurName
             << "应答请求方:" << caTarName;
    int ret = OperateDB::getInstance().handleAddFriend(caCurName, caTarName);
    qDebug() << "添加好友处理结果:" << ret;
    if(ret == 1){
        MyTcpServer::getInstance().resend(caTarName, pdu);
        return NULL;
    }
    PDU* resPdu = mkPDU(ENUM_MSG_TYPE_ADD_FRIEND_RESPOND, 0);
    memcpy(resPdu->caData, &ret, sizeof(ret));
    return resPdu;
}

PDU* ReqHandler::addFriendAgree()
{
    char caCurName[32] = { '\0' };
    char caTarName[32] = { '\0' };
    memcpy(caCurName, pdu->caData     , 32);
    memcpy(caTarName, pdu->caData + 32, 32);

    OperateDB::getInstance().handleAddFriendAgree(caCurName, caTarName);

    PDU* resPdu = mkPDU(ENUM_MSG_TYPE_ADD_FRIEND_AGREE_RESPOND, 0);

    MyTcpServer::getInstance().resend(caCurName, resPdu);

    return resPdu;
}

PDU *ReqHandler::flushFriend(QString &m_strLoginName)
{
    QStringList res = OperateDB::getInstance().handleFlushFriend(m_strLoginName.toStdString().c_str());
    uint uiMsgLen = res.size() * 32;
    PDU* resPdu = mkPDU(ENUM_MSG_TYPE_FLUSH_FRIEND_RESPOND, uiMsgLen);
    for(int i = 0; i < res.size(); i++)
    {
        memcpy(resPdu->caMsg + i * 32, res.at(i).toStdString().c_str(), 32);
    }
    return resPdu;
}

PDU *ReqHandler::delFriend()
{
    char caCurName[32] = { '\0' };
    char caTarName[32] = { '\0' };
    memcpy(caCurName, pdu->caData     , 32);
    memcpy(caTarName, pdu->caData + 32, 32);
    qDebug() << "删除好友->"
             << "删除方:" << caCurName
             << "被删除方:" << caTarName;
    int ret = OperateDB::getInstance().handleDelFriend(caCurName, caTarName);
    qDebug() << "删除好友处理结果:" << ret;

    PDU* resPdu = mkPDU(ENUM_MSG_TYPE_DELETE_FRIEND_RESPOND, 0);
    memcpy(resPdu->caData, &ret, sizeof(ret));
    return resPdu;
}

PDU *ReqHandler::chat()
{
    char caTarName[32] = { '\0' };
    memcpy(caTarName, pdu->caData + 32, 32);
    PDU* respdu = mkPDU(ENUM_MSG_TYPE_CHAT_RESPOND, pdu->uiMsgLen);
    memcpy(respdu->caData, pdu->caData, 64);
    memcpy(respdu->caMsg , pdu->caMsg , pdu->uiMsgLen);
    MyTcpServer::getInstance().resend(caTarName, respdu);
    return respdu;
}

PDU *ReqHandler::mkDir()
{
    char caDirName[32] = { '\0' };
    memcpy(caDirName, pdu->caData , 32);
    PDU* respdu = mkPDU(ENUM_MSG_TYPE_MKDIR_RESPOND, 0);
    QString strNewPath = QString("%1/%2").arg(pdu->caMsg).arg(caDirName);
    QDir dir;
    bool res = false;
    if(!dir.exists(pdu->caMsg)){ //当前路径不存在
        qDebug() << "当前路径:" << pdu->caMsg << "->(不存在)";
        memcpy(respdu->caData, &res, sizeof(res));
        return respdu;
    }
    if(dir.exists(strNewPath) || !dir.mkdir(strNewPath)){ //路径存在
        qDebug() << "新建路径:" << strNewPath << "->(已经存在)";
        memcpy(respdu->caData, &res, sizeof(res));
        return respdu;
    }
    res = true;
    memcpy(respdu->caData, &res, sizeof(res));
    qDebug() << "路径" << strNewPath << "->(创建成功)";
    return respdu;
}
